<script lang="ts" setup>
import { reactive, ref } from 'vue'
import router from '@/router/index'
import { useRoute } from 'vue-router'
import type { FormInstance, FormRules } from 'element-plus'
import { AuthStore } from '@/stores/modules/auth'
import HciDialog from '@/components/sofastui/HciDialog/HciDialog.vue'

interface LoginFormModel {
  username: string
  password: string
  checked: boolean
}

const authStore = AuthStore()
const route = useRoute()
const loginFormRef = ref<FormInstance>()

const loginFormModel = reactive<LoginFormModel>({
  username: 'root',
  password: '123456',
  checked: false,
})

const loginFormRules = reactive<FormRules<LoginFormModel>>({
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur',
    },
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur',
    },
  ],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      console.log('submit!')

      const res = await authStore.doLogin({
        type: 'username',
        data: {
          readme: true,
          remember: true,
          username: loginFormModel.username,
          password: loginFormModel.password,
          vcode: '111222',
        },
      })
      if (!res) return
      console.log(res)
      const aa = await authStore.updatePerms()
      console.log(aa)
      router.push((route.query.redirect as string) || '/')
    } else {
      console.log('error submit!')
    }
  })
}
</script>

<template>
  <div class="rounded-lg overflow-hidden shadow-lg">
    <div style="height: 150px; background-color: #027aff"></div>
    <div class="p-8 bg-white">
      <el-form
        ref="loginFormRef"
        :model="loginFormModel"
        :rules="loginFormRules"
        label-width="0"
        size="large"
        :show-message="false"
        inline-message
        status-icon
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginFormModel.username"
            type="username"
            autocomplete="off"
            prefix-icon="User"
            placeholder="请输入账号"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginFormModel.password"
            type="password"
            autocomplete="off"
            prefix-icon="Lock"
            placeholder="请输入密码"
            show-password
            clearable
          />
        </el-form-item>
        <el-form-item>
          <div class="flex justify-between w-full">
            <el-checkbox v-model="loginFormModel.checked" label="记住密码" />
            <div>
              <el-link type="primary" underline="never" @click="router.push('/account/register')"
                >去注册</el-link
              >
              <el-divider direction="vertical" />
              <el-link type="primary" underline="never" @click="router.push('/account/forget')"
                >忘记密码</el-link
              >
            </div>
          </div>
        </el-form-item>
        <el-form-item style="margin: 0">
          <el-button type="primary" class="w-full" @click="submitForm(loginFormRef)">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <HciDialog />
  </div>
</template>
